// orderlist.js
var cf = require("../../../config.js");
//获取应用实例
var app = getApp();
var util = require("../../../utils/util.js");
var baseHandle = require("../../template/baseHandle.js");
var cusmallToken = wx.getStorageSync('cusmallToken');
Page(Object.assign({}, baseHandle, {

  /**
   * 页面的初始数据
   */
  data: {
    status: "",
    app: app,
    size: 20,
    staticResPath: cf.config.staticResPath,
    userImagePath: cf.config.userImagePath,
    orderList: {},
    needUserInfo: true,
    orderListTotal: 0,
    goodsList: [],
    cancleOrderId: 0,
    siteType: "dianshang",//店铺类型：如预约为yuyue
    orderInfoUrl: "/pages/shopping_mall/orderinfo/orderinfo",
    menusSta: [true, true, true],
    showPopup: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideShareMenu();
    cusmallToken = wx.getStorageSync('cusmallToken');
    let ctx = this;

    var pageTitle = "我的订单";

    wx.setNavigationBarTitle({
      title: pageTitle
    })
    //console.log(options.status)
    wx.hideShareMenu();
    this.setData({
      status: options.status || "",
    });
    ctx.fetchData(1);

    this.setData({ "pagesLen": getCurrentPages().length });
  },
  fetchData: function (page) {
    let that = this;
    wx.showLoading({
      title: '加载中',
    });
    wx.request({
      url: cf.config.pageDomain + '/applet/mobile/visitcard/order/findEmployeeOrderList',
      data: {
        cusmallToken: cusmallToken,
        status: that.data.status,
        start: (page - 1) * that.data.size,
        type: 1,
        limit: that.data.size
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        var goodsList = [];
        if (res.data.ret == 0) {
          var list = res.data.model.list;
          for (var i = 0; i < list.length; i++) {
            var goods = list[i].orderItems;
            for (var j = 0; j < goods.length; j++) {
              goods[j].price = (goods[j].price / 100).toFixed(2);
              //goods[j].thumbnail = goods[j].picture.split(",")[0];
            }
            goodsList.push(goods);
          }
          for (var k = 0; k < list.length; k++) {
            list[k].allPrice = ((list[k].orderAmountTotal + list[k].deliveryFee - list[k].ticketPrice) / 100).toFixed(2);
            list[k].totalPrice = (list[k].orderAmountTotal / 100).toFixed(2);
            list[k].deliveryPrice = (list[k].deliveryFee / 100).toFixed(2);
            //list[k].allPrice = (list[k].deliveryPrice/100).toFixed(2);
          }
          that.setData({
            orderList: list,
            orderListTotal: res.data.model.total,
            goodsList: goodsList
          });
          //console.log(goodsList);
          wx.hideLoading();
        } else {
          wx.hideLoading();
          wx.showModal({
            title: '获取商品信息异常',
            showCancel: false,
            content: res.data.msg
          })
        }
      }
    })
  },

  getOrderList: function (event) {
    var that = this;
    this.setData({ status: event.target.dataset.status });
    that.fetchData(1);
  },
  handleMainNav: function (e) {
    var that = this;
    var siteType = e.target.dataset.type;
    wx.redirectTo({
      url: 'orderlist?sitetype=' + siteType,
    })
  },
  goToPay: function (e) {
    let that = this;
    var orderid = e.target.dataset.orderid;
    wx.navigateTo({
      url: '/pages/shopping_mall/orderpay/orderpay?id=' + orderid + ''
    })
  },
  goToOrderDetail: function (e) {
    let that = this;
    var orderid = e.target.dataset.orderid;
    wx.navigateTo({
      url: './orderInfo?orderid=' + orderid + ''
    })
  },
  modalTap: function (e) {
    var that = this;
    this.setData({ cancleOrderId: e.target.dataset.cancleid });
    //console.log(e.target);
    wx.showModal({
      title: "温馨提示",
      content: "您确认取消此商品订单么？",
      confirmText: "确定",
      cancelText: "取消",
      success: function (res) {
        if (res.confirm) {
          that.cancelOrder();
        }
      }
    })
  },
  cancelOrder: function () {
    var that = this;
    wx.showLoading({
      title: '处理中',
    });
    wx.request({
      url: cf.config.pageDomain + '/applet/mobile/visitcard/order/cancel',
      data: {
        cusmallToken: cusmallToken,
        id: that.data.cancleOrderId
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        if (res.data.ret == 0) {
          wx.hideLoading();
          wx.showModal({
            showCancel: false,
            content: "订单取消成功",
            success: function (ress) {
              if (ress.confirm) {
                that.fetchData(1);
              }
            }
          })
        } else {
          wx.hideLoading();
          wx.showModal({
            title: '订单取消失败',
            showCancel: false,
            content: res.data.msg
          })
        }
      }
    })
  },
  togglePopup: function () {
    this.setData({
      showPopup: !this.data.showPopup
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角
   */
  onShareAppMessage: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  }
}))